//------------------------------------------------------------------------
//
//	CCGameBox is a game development library for iPhone and iPod Touch.
//
//	Licensed under the BSD license, see LICENSE in root for details.
// 
//	Copyright (c) 2009 James Hui (a.k.a. Dr.Watson)
// 
//	For latest updates, please visit http://www.cocoachina.com
//
//------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include "math.h"
#include <sys/time.h>
#include <time.h>
#include <mach/mach_time.h>
#include "CCGameBox.h"
#include "GameApp.h"

#define PI 3.1415926


GameApp::GameApp()
{
	CCRenderBox *renderBox = CCRenderBox::GetInstance();
	srandom((unsigned)(mach_absolute_time() & 0xFFFFFFFF));
	
	
	
	//SCENE BUILDING
	//_tBg = renderBox->LoadTexture(kBg);
//	_bg = new CCImage(_tBg);
//	
//	_tClouds = renderBox->LoadTexture(kClouds);
//	_clouds[0] = new CCImage(_tClouds);
//	_clouds[1] = new CCImage(_tClouds);
//
//	_tCity = renderBox->LoadTexture(kCity);
//	_city = new CCImage(_tCity);
//	
//	_tCrowdB = renderBox->LoadTexture(kCrowdBack);
//	_crowdB = new CCImage(_tCrowdB);
//	_tCrowdM = renderBox->LoadTexture(kCrowdMiddle);
//	_crowdM = new CCImage(_tCrowdM);
//	_tCrowdF = renderBox->LoadTexture(kCrowdFront);
//	_crowdF[0] = new CCImage(_tCrowdF,65,60,305,160);
//	_crowdF[1] = new CCImage(_tCrowdF,370,60,305,160);
//	
//	_tGrey = renderBox->LoadTexture(kGreyZombie);
//	_grey[0] = new CCImage(_tGrey,15,125,115,380);
//	_grey[1] = new CCImage(_tGrey,178,125,115,380);
//	_grey[2] = new CCImage(_tGrey,340,125,120,380);
//	_grey[3] = new CCImage(_tGrey,502,125,125,380);
//	_grey[4] = new CCImage(_tGrey,665,125,120,380);
	
	
}

void GameApp::BeginLayout()
{
	
	
	//INIT TIME WHEN "GameApp" CREATED
	timeval launch;
	gettimeofday(&launch, NULL);
	_clouds_t		= launch;
	_crowd_t		= launch;
	_begin_t		= launch;
	_zombie_t	= launch;
	_zombieAppear_t = launch;
	
	_beginRender = true;
}

GameApp::~GameApp()
{
	SAFE_DELETE(_tBg);
	SAFE_DELETE(_bg);
	
	SAFE_DELETE(_tClouds);
	delete []_clouds;
	
	SAFE_DELETE(_tCity);
	SAFE_DELETE(_city);
	
	SAFE_DELETE(_tCrowdB);
	SAFE_DELETE(_crowdB);
	SAFE_DELETE(_tCrowdM)
	SAFE_DELETE(_crowdM);
	SAFE_DELETE(_tCrowdF);
	delete []_crowdF;
	
	SAFE_DELETE(_tGrey);
	delete []_grey;
	
}

void GameApp::Update(float dt)
{
	if (_beginRender) 
	{
		timeval now;
		gettimeofday(&now,NULL);
		//double diffcoulds_t		= (now.tv_sec*1000000+now.tv_usec) - (_clouds_t.tv_sec*1000000+_clouds_t .tv_usec);
//		double diffcrowd_t		= (now.tv_sec*1000000+now.tv_usec) - (_crowd_t.tv_sec*1000000+_crowd_t.tv_usec);
//		double diffbegin_t		= (now.tv_sec*1000000+now.tv_usec) - (_begin_t.tv_sec*1000000+_begin_t.tv_usec);
//		double diffzombie_t	= (now.tv_sec*1000000+now.tv_usec) - (_zombie_t.tv_sec*1000000+_zombie_t.tv_usec);
//		double diffzombieAppear_t = (now.tv_sec*1000000+now.tv_usec) - (_zombieAppear_t.tv_sec*1000000+_zombieAppear_t.tv_usec);
//		
//		this->CloudsAnimation(diffcoulds_t,now);
//		
//		this->CrowdAnimation(diffcrowd_t,now);
		
		
	}
}


void GameApp::Render()
{
	if (_beginRender) 
	{
		CCRenderBox *renderBox = CCRenderBox::GetInstance();
		
	//	renderBox->RenderImage(_bg, 0, 0,0.0f,kBgScaleX,kBgScaleY);
		
	}
}



//void GameApp::CreateZombie(int count,short type)
//{
//	for (int i = 0; i<count; ++i) 
//	{
//		
//	}
//}



void GameApp::TouchesBegan(float x,float y)
{	
	
}


void GameApp::TouchesMoved(float x,float y)
{
	
}


void GameApp::TouchesEnded(float x,float y)
{
}

bool GameApp::PointInRect(float pX, float pY, 
						  float minX,float minY,
						  float maxX,float maxY)
{
	if (pX >= minX && pX <= maxX && pY >= minY && pY <= maxY) return true;
	else return false;
}


